<?php
use Yansongda\Pay\Pay;
use jianyan\excel\Excel;
ini_set('memory_limit', '1024M');
require_once("./vendor/autoload.php");
require_once("./lib/fun.php");
require_once("./lib/pdo.php");
$param = I("param.");
$token = str_replace(" ","+",I("token"));
$uid = authcode($token,'DECODE',"youluda",7*24*3600);
if(!$uid){
    json([
        'code' => 2,
        'msg'  => '请先登录一下',
    ]);
}
if(empty($_FILES['file'])){
    json([
        'code' => 0,
        'msg'  => '请上传文件',
    ]);
}
$list = Excel::import($_FILES['file']['tmp_name'],2);
$db = db('lu_customer');
// dump($list);die;
$add_data = [];
$str = "";
$unit = db('lu_unit')->select();
$unit = returnArrByKey($unit,"name");

foreach($list as $k => $val){
    if($k > 0 && $val[1]){
         if(empty($val[1])){
             $str .= "第{$k}行地点不能为空,";
            
        }
        if(empty($val[5])){
            $str .= "第{$k}行详情地址不能为空,";
            
        }
        if($val[24] > 0){
            if(empty($val[25])){
                $str .= "第{$k}行货物单位不能为空,";
            }
        }
        if($val[25]){
            if(!isset($unit[$val[25]])){
                $str .= "第{$k}行货物单位还不支持,";
            }else{
                $val[25] = $unit[$val[25]]['id'];
            }
            
        }
         if($val[9]){
            $val[9] = str_replace("，",",",$val[9]);
            $location_arr = explode(",",$val[9]);
            if(empty($location_arr[0]) || empty($location_arr[1])){
                $str .= "第{$k}行坐标格式不对,";
                
            }
            $add = [
                'uid' => $uid,
                'group_id' => $param['group_id'] ?? 0,
                'address_name' => $val[1],
                'address_name' => $val[1],
                'province' => $val[2] ?? "",
                'city' => $val[3] ?? "",
                'country' => $val[4] ?? "",
                'position'  => $val[5] ?? "",
                'lng' =>  $location_arr[0] ?? "",
                'lat' =>  $location_arr[1] ?? "",
                'mobile' => $val[7] ?? "",
                'name' => $val[6] ?? "",
                'ext' => $val[8] ?? "",
                'manage' => $val[10] ?? "",
                'order_no' => $val[11] ?? "",
                'goods_no' => $val[12] ?? "",
                'shop_info' => $val[13] ?? "",
                'user_no' => $val[14] ?? "",
                'goods_name' => $val[15] ?? "",
                'spec' => $val[16] ?? "",
                'unit' => $val[17] ?? "",
                'goods_num' => $val[18] ?? "",
                'price' => $val[19] ?? 0.00,
                'total_price' => $val[20] ?? 0.00,
                'fee' => $val[21] ?? 0.00,
                'order_status' => $val[22] ?? "",
                'order_ext' => $val[23] ?? "",
                'store_num' => $val[24] ?? 0.0,
                'store_unit' => $val[25] ?? 0,
                'add_time' => ftime(),
            ];
            $add_data[] = $add;
        }else{
            $detail_url = $val['2'].$val[3].$val[4].$val[5];
            $location = gd_url($detail_url);
            if(empty($location['lng']) || empty($location['lat'])){
                $str .= "第{$k}行详情地点无法解析成坐标,";
                 
            }
            $add = [
                'uid' => $uid,
                'group_id' => $param['group_id'] ?? 0,
                'address_name' => $val[1],
                'province' => $val[2] ?? "",
                'city' => $val[3] ?? "",
                'country' => $val[4] ?? "",
                'position'  => $val[5] ?? "",
                'lng' => $location['lng'] ?? "",
                'lat' => $location['lat'] ?? "",
                'mobile' => $val[7] ?? "",
                'name' => $val[6] ?? "",
                'ext' => $val[8] ?? "",
                'manage' => $val[10] ?? "",
                'order_no' => $val[11] ?? "",
                'goods_no' => $val[12] ?? "",
                'shop_info' => $val[13] ?? "",
                'user_no' => $val[14] ?? "",
                'goods_name' => $val[15] ?? "",
                'spec' => $val[16] ?? "",
                'unit' => $val[17] ?? "",
                'goods_num' => $val[18] ?? "",
                'price' => $val[19] ?? 0.00,
                'total_price' => $val[20] ?? 0.00,
                'fee' => $val[21] ?? 0.00,
                'order_status' => $val[22] ?? "",
                'order_ext' => $val[23] ?? "",
                'store_num' => $val[24] ?? 0.0,
                'store_unit' => $val[25] ?? 0,
                'add_time' => ftime(),
            ];
             $add_data[] = $add;
        }
        
        
    }else{
         if($k > 0){
            $str .= "第{$k}行地点名称不能为空,";
        }
    }
}
if($str){
    json([
            'code' => 0,
            'msg'  => $str."请重新检查文档格式内容,严格按照使用模板导入.",
        ]); 
}
foreach($add_data as $v){
    $res = $db->add($v);
}
if($res){
    json([
            'code' => 1,
            'msg'  => '提交成功',
        ]);
}else{
    json([
            'code' => 0,
            'msg'  => '文档内含非法字符无法识别,请重新检查文档格式内容,严格按照使用模板导入.',
        ]); 
}
